﻿$(function () {
    //初始化控件
    laydate.render({ elem: '#txtlivePtime', type: 'datetime' });
    laydate.render({ elem: '#txtexcStartTime', type: 'datetime' });
    laydate.render({ elem: '#txtexitPtime', type: 'datetime' });
    position();
    SetValue();
    $("#btnSave").on("click", function () { savemr(); });
    $("#btnPrint").on("click", function () { printmr(); });
    $("#btnDel").on("click", function () { delmr(); });
    $("#btnHisback").on("click", function () { window.location.href = "/StrokeCenter/MedicalRecords/Index"; });
    $("#btnTimePath").on("click", function () { if (IsEmpty($("#txtPatientId").val())) return; window.location.href = "/StrokeCenter/MedicalRecords/Timeline?pid=" + $("#txtPatientId").val(); });
    $("#btnTimeLine").on("click", function () {
        if (IsEmpty($("#txtPatientId").val())) return;
        var url = "/StrokeCenter/Pathway/Index?id=" + $("#txtPatientId").val();
        window.parent.top.tab.addTabItem({ tabid: "time" + $("#txtPatientId").val(), text: "时间轴", url: url });
    });
    if (GetQueryString("review") == 1) {
        $("#btnReview").show();
        $("#btnReview").on("click", function () {
            $.ligerDialog.open({
                height: 300,
                width: 320,
                title: '病历审核',
                url: '/StrokeCenter/MedicalRecords/Review',
                data: { pid: $("#txtPatientId").val() },
            });
        });
    }
    $(":text[typeof=float]").keyup(function () {
        var c = $(this);
        if (/[^\d|\.]/.test(c.val())) {
            var num = c.val().replace(/[^\d|\.]/g, '');
            $(this).val(num == "" ? "" : parseInt(num));
        }
    }).blur(function () {
        if (!/^\d+(\.\d{0,2})?$/.test($(this).val()))
            $(this).val("0.00");
    });
    $(":text[typeof=number]").keyup(function () {
        var c = $(this);
        if (/[^\d]/.test(c.val())) {
            var num = c.val().replace(/[^\d]/g, '');
            $(this).val(num == "" ? "" : parseInt(num));
        }
    }).blur(function () {
        if (!/\d+/.test($(this).val()))
            $(this).val("0");
    });
    //计算BMI
    $("#txtpheigh,#txtpweight").on("blur", function () {
        var float2regex = /^\d+(\.\d{0,2})?$/;
        var height = $("#txtpheigh").val();
        var weight = $("#txtpweight").val();
        if (!float2regex.test(height)) return;
        if (!float2regex.test(weight)) return;
        if (height == 0) return;
        //计算BMI
        var height2 = parseInt(height) / 100;
        var bmiVal = (parseFloat(weight) / (height2 * height2)).toFixed(2);
        $("#txtbmiVal").val(bmiVal);
    });
    $('input[name="rdocomeinismRs"]').click(function () {
        if ($("input[name='rdocomeinismRs']:checked").val() == 1) {
            $("#rdocomeinismRsYes").show();
        } else {
            $("#rdocomeinismRsYes").hide();
            $("#txtcomeinmRs").val("");
        }
    });
    $('input[name="rdocomeinisNihss"]').click(function () {
        if ($("input[name='rdocomeinisNihss']:checked").val() == 1) {
            $("#rdocomeinisNihssYes").show();
        } else {
            $("#rdocomeinisNihssYes").hide();
            $("#txtcomeinNihss").val("");
        }
    });
    $('input[name="rdotygnpgischk"]').click(function () {
        if ($("input[name='rdotygnpgischk']:checked").val() == 1) {
            $("#txtwtyssyValYes").show();
        } else {
            $("#txtwtyssyValYes").hide();
            $("#txtwtyssyVal").val('');
        }
    });
    $('input[name="rdoreshavexz"]').click(function () {
        if ($("input[name='rdoreshavexz']:checked").val() == 1) {
            $("#rdoreshavexzYes1").show();
            $("#rdoreshavexzYes2").show();
            $("#rdoreshavexzYes3").show();
        } else {
            $("#rdoreshavexzYes1").hide();
            $("#rdoreshavexzYes2").hide();
            $("#rdoreshavexzYes3").hide();
        }
    });
    $('input[name="rdoshistzyw"]').click(function () {
        if ($("input[name='rdoshistzyw']:checked").val() == 1) {
            $("#rdoshistzywYes1").show();
            $("#rdoshistzywYes2").show();
        } else {
            $("#rdoshistzywYes1").hide();
            $("#rdoshistzywYes2").hide();

            $("input[name=cbxshtzywType]").prop("checked", false);
            $("input[name=cbxshtzywType]").parent().removeClass("checked");
        }
    });
    $('input[name="rdoshisjtyw"]').click(function () {
        if ($("input[name='rdoshisjtyw']:checked").val() == 1) {
            $("#rdoshisjtywYes1").show();
            $("#rdoshisjtywYes2").show();
        } else {
            $("#rdoshisjtywYes1").hide();
            $("#rdoshisjtywYes2").hide();

            $("input[name=chxshjtywType]").prop("checked", false);
            $("input[name=chxshjtywType]").parent().removeClass("checked");
        }
    });
    $('input[name="rdoheathisxj"]').click(function () {
        if ($("input[name='rdoheathisxj']:checked").val() == 1) {
            $("#rdoheathisxjYes").show();
        } else {
            $("#rdoheathisxjYes").hide();
            
            $("input[name=cbxheathxjType]").prop("checked", false);
            $("input[name=cbxheathxjType]").parent().removeClass("checked");
        }
    });
    $('input[name="rdoexitismRs"]').click(function () {
        if ($("input[name='rdoexitismRs']:checked").val() == 1) {
            $("#rdoexitismRsYes").show();
        } else {
            $("#rdoexitismRsYes").hide();
            $("#txtexitmRsVal").val("");
        }
    });
    $('input[name="rdoexitisHihss"]').click(function () {
        if ($("input[name='rdoexitisHihss']:checked").val() == 1) {
            $("#rdoexitisHihssYes").show();
        } else {
            $("#rdoexitisHihssYes").hide();
            $("#txtexitHihssVal").val("");
        }
    });
    //单选框
    $("input[type=radio]").parent().addClass("radio_label");
    $("input[type=radio][disabled!=disabled]").click(function (e) {
        $("input[type=radio][name='" + $(this).attr('name') + "']").parent().removeClass("checked");
        $(this).parent().addClass("checked");
    });
    $("input[type=radio][checked=checked]").parent().addClass("checked");
    if (navigator.userAgent.indexOf("Firefox") > 0) { $(":radio,:checkbox").hide(); }
    //复选框	 
    $("input[type=checkbox]").parent().addClass("checkbox_label");
    $("input[type=checkbox][disabled!=disabled]").click(function (e) {
        if ($(this).parent().hasClass("checked"))
            $(this).parent().removeClass("checked");
        else
            $(this).parent().addClass("checked");
    });
    $("input[type=checkbox][checked=checked]").parent().addClass("checked");
});
function position() {
    LiftEffect({
        "control1": ".lift-nav",
        "control2": ".lift",
        "target": [".t0", ".t1", ".t2", ".t3", ".t4", ".t5", ".t6"],
        "current": "current"
    });
}
function printmr() {
    if ($("#txtPatientId").val() == "") return;
    window.parent.top.tab.addTabItem({ tabid: "print" + $("#txtPatientId").val(), text: "病历-" + $("#txtrealName").val(), url: "/StrokeCenter/MedicalRecords/CeacasmrPrint?pid=" + $("#txtPatientId").val() });
}
function delmr() {
    if ($("#txtPatientId").val() == "") return;
    $.ligerDialog.confirm('确定删除该患者吗', function (yes) {
        if (yes) {
            var url = "/StrokeCenter/PatientList/GiveUp";
            $.get(url, {
                rnd: new Date().getMilliseconds(),
                patientId: $("#txtPatientId").val()
            }, function (data, textStatus) {
                if (textStatus == "success") {
                    var d = $.parseJSON(data);
                    if (d.Result == 1) {
                        $.ligerDialog.success("操作成功","", function () {
                            window.location.href = "/StrokeCenter/MedicalRecords/index";
                        });
                    }
                    else if (d.Result == 0) {
                        $.ligerDialog.error(d.Body);
                    }
                    else {
                        $.ligerDialog.error("程序错误");
                    }
                }
            })
        }
    });
}
function savemr() {
    if ($("#txtPatientId").val() == "") return;
    var data = {};
    $.each(FiledArray, function (i, a) {
        var datadetail = {};
        $.each(a.column, function (j, b) {
            var str = "";
            if (b.type == "text" || b.type == 'date') {
                str = $('#' + b.code).val();
                $("input[name='" + b.code + "']").blur();
            } else if (b.type == "radio") {
                str = $("input[name='" + b.code + "']:checked").val();
            } else if (b.type == "checkbox") {
                var str = CheckBoxToArrStr(b.code);
            }
            datadetail[b.tocode] = str == "" ? b.value : str;
        })
        data[a.table] = datadetail;
    })
    $.post("/StrokeCenter/MedicalRecords/SaveCeacasmr", { datajson: JSON.stringify(data), rnd: getRnd() }, function (data, textStatus) {
        if (textStatus == "success") {
            var d = $.parseJSON(data);
            if (d.Result == 1) {
                $.ligerDialog.success("保存成功","", function () {
                    window.location.reload(true);
                });
            }
            else if (d.Result == 0) {
                $.ligerDialog.error(d.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        }
    })
}

function SetValue() {
    var patientid = $('#txtPatientId').val();
    if (IsEmpty(patientid)) {
        $.ligerDialog.error("获取数据失败！");
    }
    waitting1 = $.ligerDialog.waitting('加载中，请稍候...');
    $.ajax({
        url: '/StrokeCenter/MedicalRecords/GetCEACAS?patientid=' + patientid + '&rnd=' + getRnd(),
        cache: false,
        dataType: 'json',
        success: function (data) {
            $.each(FiledArray, function (i, a) {
                var datadetail = eval("data." + a.table);
                if (datadetail != undefined && datadetail != null) {
                    $.each(a.column, function (j, b) {
                        if (b.type == "text") {
                            $('#' + b.code).val(eval("datadetail." + b.tocode));
                        } else if (b.type == "date") {
                            $('#' + b.code).val(obj2Timemm(eval("datadetail." + b.tocode)));
                        } else if (b.type == "radio") {
                            RadioSetVal(b.code, eval("datadetail." + b.tocode), true);
                            $("input[name='" + b.code + "'][value='" + eval("datadetail." + b.tocode) + "']").click();
                        } else if (b.type == "checkbox") {
                            CheckBoxSetVal(b.code, eval("datadetail." + b.tocode),true);
                        }
                    })
                }
            })
        },
        complete: function () {
            waitting1.close();
        }
    });
}
var FiledArray = [
    {
        table: "patientInfo",
        column: [
            { name: "ID", code: "txtPIId", tocode: "ID", value: "0", type: "text" },
            { name: "姓名", code: "txtrealName", tocode: "PATIENT_NAME", value: "", type: "text" },
            { name: "性别", code: "rdogender", tocode: "PATIENT_SEX", value: "", type: "radio" },
            { name: "身份证", code: "txtIDcard", tocode: "PATIENT_ID_NUM", value: "", type: "text" },
            { name: "住院号", code: "txtlivePid", tocode: "Hospital_ID", value: "", type: "text" },
            //{ name: "病案号", code: "txtpataId", tocode: "PATIENT_ID", value: "", type: "text" },
            { name: "病人编号", code: "txtpatId", tocode: "PATIENT_ID", value: "", type: "text" },
            { name: "来院方式", code: "comeType", tocode: "PATIENT_ARRIVE_CLASS", value: "", type: "radio" }
        ]
    },
    {
        table: "ambulanceInfo",
        column: [
            { name: "ID", code: "txtAIId", tocode: "ID", value: "0", type: "text" },
            { name: "txtpatId", code: "txtpatId", tocode: "PATIENT_ID", value: "", type: "text" },
            { name: "住院时间", code: "txtlivePtime", tocode: "AdmissionTime", value: "", type: "date" },
            { name: "入院途径", code: "rdocomeinType", tocode: "AdmissionPath", value: "", type: "radio" }
        ]
    },
    {
        table: "patientCasces",
        column: [
            { name: "ID", code: "txtPCId", tocode: "ID", value: "0", type: "text" },
            { name: "txtpatId", code: "txtpatId", tocode: "Patient_ID", value: "", type: "text" },
            { name: "入院mRS评分", code: "rdocomeinismRs", tocode: "Assessment_MRS_Bool", value: "0", type: "radio" },
            { name: "评分分数", code: "txtcomeinmRs", tocode: "Assessment_MRS_Score", value: "", type: "text" },
            { name: "入院NIHSS评分", code: "rdocomeinisNihss", tocode: "Assessment_Nihss_Bool", value: "0", type: "radio" },
            { name: "评分分数", code: "txtcomeinNihss", tocode: "Assessment_Nihss_Score", value: "", type: "text" },
            { name: "吞咽功能评估", code: "rdotygnpgischk", tocode: "Assessment_Swallowing_Bool", value: "0", type: "radio" },
            { name: "洼田饮水实验", code: "txtwtyssyVal", tocode: "Assessment_Swallowing_Content", value: "", type: "text" },
            { name: "是否有症状", code: "rdoishaveill", tocode: "Assessment_Symptoms_Bool", value: "0", type: "radio" },
            { name: "颈部血管检查", code: "cbxjbxgcschk", tocode: "Assessment_NeckBloodVessel_Type", value: "", type: "checkbox" },
            { name: "结果/是否有狭窄", code: "rdoreshavexz", tocode: "Assessment_NeckBloodVessel_Bool", value: "0", type: "radio" },
            { name: "狭窄部位：左侧", code: "rdohavexzbodyareal", tocode: "Assessment_NarrowAreaLeft_Type", value: "0", type: "radio" },
            { name: "狭窄部位：右侧", code: "rdohavexzbodyarear", tocode: "Assessment_NarrowAreaRight_Type", value: "0", type: "radio" },

            { name: "是否有症状", code: "rdoishaveillinExc", tocode: "Surgery_Symptoms_Bool", value: "0", type: "radio" },
            { name: "手术开始时间", code: "txtexcStartTime", tocode: "Surgery_Start_Time", value: "", type: "date" },
            { name: "手术部位", code: "cbxexcBodyPostion", tocode: "Surgery_Location_Type", value: "", type: "checkbox" },
            { name: "麻醉方式", code: "rdoexcpmzType", tocode: "Surgery_Anesthesia_Type", value: "0", type: "radio" },
            { name: "实施监测手段", code: "cbxexcssjcType", tocode: "Surgery_Detection_Type", value: "", type: "checkbox" },
            { name: "手术采取方式", code: "rdoexcGetType", tocode: "Surgery_Type", value: "0", type: "radio" },
            { name: "是否采用补片", code: "rdoexcisUsebps", tocode: "Surgery_Patch_Bool", value: "0", type: "radio" },
            { name: "并发症", code: "cbxexchavebfzs", tocode: "Surgery_Complications_Type", value: "", type: "checkbox" },

            { name: "抗血小板药物", code: "rdoshiskxxbyw", tocode: "Drug_KXXBYW_Bool", value: "0", type: "radio" },
            { name: "抗凝药物", code: "rdoshisknyw", tocode: "Drug_KNYW_Bool", value: "0", type: "radio" },
            { name: "降压药物", code: "rdoshisjyyw", tocode: "Drug_JYYW_Bool", value: "0", type: "radio" },
            { name: "调脂药物", code: "rdoshistzyw", tocode: "Drug_TZYW_Bool", value: "0", type: "radio" },
            { name: "调脂药物种类", code: "cbxshtzywType", tocode: "Drug_TZYW_Type", value: "", type: "checkbox" },
            { name: "降糖药物", code: "rdoshisjtyw", tocode: "Drug_JTYW_Bool", value: "0", type: "radio" },
            { name: "降糖药物种类", code: "chxshjtywType", tocode: "Drug_JTYW_Type", value: "", type: "checkbox" },

            { name: "健康宣教", code: "rdoheathisxj", tocode: "Health_Education_Bool", value: "0", type: "radio" },
            { name: "宣教方式", code: "cbxheathxjType", tocode: "Health_Education_Type", value: "", type: "checkbox" },

            { name: "出院时间", code: "txtexitPtime", tocode: "Hospital_Out_Time", value: "", type: "date" },
            { name: "离院方式", code: "rdoexitPType", tocode: "Hospital_Out_Type", value: "0", type: "radio" },
            { name: "出院mRS评分", code: "rdoexitismRs", tocode: "Hospital_MRS_Bool", value: "0", type: "radio" },
            { name: "评分", code: "txtexitmRsVal", tocode: "Hospital_MRS_Score", value: "", type: "text" },
            { name: "出院NIHSS评分", code: "rdoexitisHihss", tocode: "Hospital_Nihss_Bool", value: "0", type: "radio" },
            { name: "评分", code: "txtexitHihssVal", tocode: "Hospital_Nihss_Score", value: "", type: "text" },
            { name: "出院带药", code: "cbxexitpwithy", tocode: "Hospital_Drug_Bool", value: "", type: "checkbox" }
        ]
    },
    {
        table: "patientSigns",
        column: [
            { name: "ID", code: "txtPSId", tocode: "PATIENT_SIGNS_ID", value: "0", type: "text" },
            { name: "txtpatId", code: "txtpatId", tocode: "PATIENT_ID", value: "", type: "text" },
            { name: "民族", code: "txtethnic", tocode: "Patient_National", value: "", type: "text" },
            { name: "医疗付款方式", code: "chkmedicalinsurance", tocode: "Patient_Pay_Type", value: "", type: "radio" },
            { name: "住院次数", code: "txtlivePcount", tocode: "Patient_Hospital_Num", value: "", type: "text" },
            { name: "BMI(kg/㎡)", code: "txtbmiVal", tocode: "Patient_BMI", value: "", type: "text" },
            { name: "脉搏(次/分)", code: "txtpmbVal", tocode: "PATIENT_HEART", value: "", type: "text" },
            { name: "收缩压(mmHg)", code: "txtpssyVal", tocode: "PATIENT_BLOOD_H", value: "", type: "text" },
            { name: "舒张压(mmHg)", code: "txtpszyVal", tocode: "PATIENT_BLOOD_L", value: "", type: "text" },
            { name: "身高(cm)", code: "txtpheigh", tocode: "Patient_Height", value: "", type: "text" },
            { name: "体重(kg)", code: "txtpweight", tocode: "Patient_Weight", value: "", type: "text" }
        ]
    }
]
